//
//  YMPDFPage.m
//  TestDemo
//
//  Created by fengxijun on 16/6/24.
//  Copyright © 2016年 goappbox. All rights reserved.
//

#import "YMPDFPage.h"

@interface YMPDFPage ()
{
    
}
@property (nonatomic) CGPDFPageRef page;
@end

// table methods to parse pdf
static void op_MP (CGPDFScannerRef s, void *info) {
    const char *name;
    if (!CGPDFScannerPopName(s, &name))
        return;
    printf("MP /%s\n", name);
}

static void op_DP (CGPDFScannerRef s, void *info) {
    const char *name;
    if (!CGPDFScannerPopName(s, &name))
        return;
    printf("DP /%s\n", name);
}

static void op_BMC (CGPDFScannerRef s, void *info) {
    const char *name;
    if (!CGPDFScannerPopName(s, &name))
        return;
    printf("BMC /%s\n", name);
}

static void op_BDC (CGPDFScannerRef s, void *info) {
    const char *name;
    if (!CGPDFScannerPopName(s, &name))
        return;
    printf("BDC /%s\n", name);
}

static void op_EMC (CGPDFScannerRef s, void *info) {
    const char *name;
    if (!CGPDFScannerPopName(s, &name))
        return;
    printf("EMC /%s\n", name);
}

@implementation YMPDFPage

@synthesize fonts=_fonts,content=_content;

- (void) dealloc {
    if (self.page) {
        CGPDFPageRelease(self.page);
    }
}

- (instancetype) initWithPage:(CGPDFPageRef)page {
    self = [super init];
    if (self) {
        self.page = CGPDFPageRetain(page);
//        [self mapOperatorTab];
    }
    return self;
}

- (void)mapOperatorTab {
    CGPDFScannerRef myScanner;
    CGPDFOperatorTableRef myTable = CGPDFOperatorTableCreate();
    CGPDFOperatorTableSetCallback (myTable, "MP", &op_MP);
    CGPDFOperatorTableSetCallback (myTable, "DP", &op_DP);
    CGPDFOperatorTableSetCallback (myTable, "BMC", &op_BMC);
    CGPDFOperatorTableSetCallback (myTable, "BDC", &op_BDC);
    CGPDFOperatorTableSetCallback (myTable, "EMC", &op_EMC);
    
    CGPDFContentStreamRef contentStream = CGPDFContentStreamCreateWithPage(self.page);
    
    myScanner = CGPDFScannerCreate(contentStream, myTable, (void *)self);
    CGPDFScannerScan(myScanner);
    
    CGPDFStringRef str;
    CGPDFDictionaryRef d = CGPDFPageGetDictionary(self.page);
    if (CGPDFDictionaryGetString(d, "Thumb", &str)){
        CFStringRef s;
        s = CGPDFStringCopyTextString(str);
        if (s != NULL) {
            //need something in here in case it cant find anything
            NSLog(@"%@ testing it", s);
        }
        CFRelease(s);
        //      CFDataRef data = CGPDFStreamCopyData (stream, CGPDFDataFormatRaw);
    }
}

- (NSUInteger)pageNumber {
    return CGPDFPageGetPageNumber(self.page);
}

- (CGRect)mediaBox {
    return CGPDFPageGetBoxRect(self.page, kCGPDFMediaBox);
}

- (CGPDFDictionaryRef)pageDictionaryForKeyPath:(NSString *)keyPath {
    NSArray *keyPaths = [keyPath componentsSeparatedByString:@"/"];
    CGPDFDictionaryRef category = CGPDFPageGetDictionary(self.page);
    for (NSString *key in keyPaths) {
        CGPDFDictionaryRef tCategory;
        CGPDFDictionaryGetDictionary(category, [key UTF8String], &tCategory);
        if (tCategory != nil) {
            category = tCategory;
        } else {
            break;
        }
    }
    
    return category;
}

- (YMPDFFonts *)fonts {
    if (_fonts == nil) {
        CGPDFDictionaryRef category = [self pageDictionaryForKeyPath:@"Resources/Font"];
        _fonts = [[YMPDFFonts alloc] initWithDictionary:category];
    }
    
    return _fonts;
}

- (YMPDFObject *)content {
    if (_content == nil) {
        _content = [[YMPDFObject alloc] initWithDictionary:CGPDFPageGetDictionary(self.page)];
    }
    
    return _content;
}

@end
